<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择数据类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.multimodal_data_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="MD5值" prop="dataKey">
        <el-input
          v-model="queryParams.dataKey"
          placeholder="请输入MD5值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="原始标题" prop="titleOrg">
        <el-input
          v-model="queryParams.titleOrg"
          placeholder="请输入原始标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="中文标题" prop="titleCn">
        <el-input
          v-model="queryParams.titleCn"
          placeholder="请输入中文标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="发布机构" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="原始url" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入原始url"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="本地存储路径" prop="localPath">
        <el-input
          v-model="queryParams.localPath"
          placeholder="请输入本地存储路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="标签" prop="labels">
        <el-input
          v-model="queryParams.labels"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="所属文章URL" prop="articleUrl">
        <el-input
          v-model="queryParams.articleUrl"
          placeholder="请输入所属文章URL"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所属文章名称" prop="article">
        <el-input
          v-model="queryParams.article"
          placeholder="请输入所属文章名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="数据源ID" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入数据源ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="后缀格式" prop="fileFormat">
        <el-input
          v-model="queryParams.fileFormat"
          placeholder="请输入后缀格式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="音视频时长" prop="lastTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.lastTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="文件大小" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入文件大小"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- 来源类型(网络0/用户上传1) -->
      <el-form-item label="来源类型" prop="sourceType">
        <el-select v-model="queryParams.sourceType" placeholder="请选择来源类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.multimodal_data_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="上传用户ID" prop="uploadUserId">
        <el-input
          v-model="queryParams.uploadUserId"
          placeholder="请输入上传用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:multimodal_file:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:multimodal_file:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:multimodal_file:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['information:multimodal_file:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="multimodal_fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="自增id" align="center" prop="id" /> -->
      <el-table-column label="名称" width="150" prop="name" />
      <el-table-column label="数据类型" align="center" prop="dataType" >
        <template slot-scope="scope">
          <span>{{ showDataType(scope.row.dataType) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="MD5值" align="center" prop="dataKey" /> -->
      <!-- <el-table-column label="avator" align="center" prop="avator" /> -->
      <el-table-column label="原始标题" align="center" prop="titleOrg" />
      <!-- <el-table-column label="中文标题" align="center" prop="titleCn" /> -->
      <!-- <el-table-column label="发布机构" align="center" prop="publisher" /> -->
      <!-- <el-table-column label="原始摘要(简介)" align="center" prop="summaryOrg" /> -->
      <!-- <el-table-column label="中文摘要" align="center" prop="summaryCn" /> -->
      <!-- <el-table-column label="原始全文" align="center" prop="contentsOrg" /> -->
      <!-- <el-table-column label="中文全文" align="center" prop="contentsCn" /> -->
      <!-- 多模态资源原始链接 -->
      <!-- <el-table-column label="原始URL" align="center" prop="url" /> -->
      <el-table-column label="原始URL" align="center" width="280">
        <template slot-scope="scope">
          <a :href=scope.row.url target="_blank" :title=scope.row.url>{{scope.row.url}}</a>
        </template>
      </el-table-column>
      <!-- TODO: 截取显示本地存储路径（如30个字符），点击单元显示全部路径 -->
      <!-- <el-table-column label="本地存储路径" align="center" prop="localPath" /> -->
      <el-table-column label="本地存储路径" align="center" width="250">
        <template slot-scope="scope">
          <a :href=scope.row.localPath target="_blank" :title=scope.row.localPath>{{showContent(scope.row.localPath)}}</a>
        </template>
      </el-table-column>

      <el-table-column label="标签" width="300" align="center" prop="labels" />
      <!-- <el-table-column label="所属文章URL" align="center" prop="articleUrl" /> -->
      <!-- <el-table-column label="所属文章名称" align="center" prop="article" /> -->
      <!-- <el-table-column label="数据源ID" align="center" prop="sourceId" /> -->
      <el-table-column label="获取时间" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="后缀格式" align="center" prop="fileFormat" />
      <!-- <el-table-column label="音视频时长" align="center" prop="lastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="文件大小" align="center" prop="size" />
      <!-- 来源类型(网络0/用户上传1) -->
      <el-table-column label="来源" align="center">
        <template slot-scope="scope">
          <span>{{ showDataSource(scope.row.sourceType) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="上传用户ID" align="center" prop="uploadUserId" /> -->
      <!-- <el-table-column label="租户ID" align="center" prop="tenantId" /> -->
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">       
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:multimodal_file:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:multimodal_file:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改多模态数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- 多模态数据展示区域 -->
        <!-- 使用video.js组件播放视频（点击取消按钮，可以关闭视频源） -->
        <el-form-item label="">
          <div v-if="showMultimodalContent(form) == 0">
            <el-image 
              style="width: 90%; height: 400px"
              :src="imgUrl"
              fit="contain"
              :preview-src-list="[imgUrl]"
              >
            </el-image>
          </div>          
          <div v-if="showMultimodalContent(form) == 1">
            <video id="casvideoplayer" ref="videoPlayerRef" controls style="width: 90%; height: 400px;">
              <source :src="playUrl" type="video/mp4">
            </video>
          </div>
          <div v-if="showMultimodalContent(form) == 2">
            <audio ref="audioPlayer" :src="audioUrl" controls></audio>
          </div>
        </el-form-item>

        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="数据类型">
          <el-select v-model="form.dataType" placeholder="请选择数据类型">
            <el-option
            v-for="dict in dict.type.multimodal_data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- 后台计算，查重 -->
        <!-- <el-form-item label="MD5值" prop="dataKey">
          <el-input v-model="form.dataKey" placeholder="请输入MD5值" />
        </el-form-item> -->
        <el-form-item label="avator" prop="avator">
          <el-input v-model="form.avator" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="原始标题" prop="titleOrg">
          <el-input v-model="form.titleOrg" placeholder="请输入原始标题" />
        </el-form-item>
        <!-- <el-form-item label="中文标题" prop="titleCn">
          <el-input v-model="form.titleCn" placeholder="请输入中文标题" />
        </el-form-item> -->
        <el-form-item label="发布机构" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入发布机构" />
        </el-form-item>
        <el-form-item label="原始摘要(介绍/描述)" prop="summaryOrg">
          <el-input v-model="form.summaryOrg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="中文摘要" prop="summaryCn">
          <el-input v-model="form.summaryCn" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="原始全文" prop="contentsOrg">
          <el-input v-model="form.contentsOrg" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="中文全文" prop="contentsCn">
          <el-input v-model="form.contentsCn" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <el-form-item label="原始url" prop="url">
          <el-input v-model="form.url" placeholder="请输入原始url" />
        </el-form-item>
        <el-form-item label="本地存储路径" prop="localPath">
          <el-input v-model="form.localPath" placeholder="请输入本地存储路径" />
        </el-form-item>
        <el-form-item label="标签" prop="labels">
          <el-input v-model="form.labels" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="所属文章URL" prop="articleUrl">
          <el-input v-model="form.articleUrl" placeholder="请输入所属文章URL" />
        </el-form-item>
        <el-form-item label="所属文章DataKey" prop="article">
          <el-input v-model="form.article" placeholder="请输入所属文章DataKey" />
        </el-form-item>
        <el-form-item label="数据源ID" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入数据源ID" />
        </el-form-item>
        <el-form-item label="后缀格式" prop="fileFormat">
          <el-input v-model="form.fileFormat" placeholder="请输入后缀格式" />
        </el-form-item>
        <!-- 后台默认 -->
        <!-- <el-form-item label="创建日期" prop="lastTime">
          <el-date-picker clearable size="small"
            v-model="form.lastTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="文件大小" prop="size">
          <el-input v-model="form.size" placeholder="请输入文件大小" />
        </el-form-item>
        <el-form-item label="来源类型" prop="sourceType">
          <el-select v-model="form.sourceType" placeholder="请选择来源类型">
            <el-option
              v-for="dict in dict.type.multimodal_data_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- 系统默认填写 -->
        <!-- <el-form-item label="上传用户ID" prop="uploadUserId">
          <el-input v-model="form.uploadUserId" placeholder="请输入上传用户ID" />
        </el-form-item> -->
        <!-- <el-form-item label="租户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户ID" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMultimodal_file, getMultimodal_file, delMultimodal_file, addMultimodal_file, updateMultimodal_file, exportMultimodal_file } from "@/api/information/multimodal_file";

export default {
  name: "Multimodal_file",
  dicts: ['multimodal_data_type', 'multimodal_data_source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 多模态数据表格数据
      multimodal_fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        dataType: null,
        dataKey: null,
        avator: null,
        titleOrg: null,
        titleCn: null,
        publisher: null,
        summaryOrg: null,
        summaryCn: null,
        contentsOrg: null,
        contentsCn: null,
        url: null,
        localPath: null,
        labels: null,
        articleUrl: null,
        article: null,
        sourceId: null,
        fileFormat: null,
        lastTime: null,
        size: null,
        sourceType: null,
        uploadUserId: null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        dataKey: [
          { required: true, message: "MD5值不能为空", trigger: "blur" }
        ],
        sourceType: [
          { required: true, message: "来源类型(网络0/用户上传1)不能为空", trigger: "change" }
        ],
        uploadUserId: [
          { required: true, message: "上传用户ID不能为空", trigger: "blur" }
        ],
      },

      //多模态数据展示
      //图像：
      imgUrl: "",
      //音频：
      audioUrl: "",
      // 视频：使用video.js播放视频配置
      videoJsPlayer: null,
      playUrl:"",           //视频播放链接
      videoPlayerOption: {
          controls: true,   //确定播放器是否具有用户可以与之交互的控件。没有控件，启动视频播放的唯一方法是使用autoplay属性或通过Player API。 
          // url: "",          //要嵌入的视频资源url（不起作用？）
          poster: '',       //封面
          autoplay: false,  //自动播放属性, true/false/"muted"(静音播放)
          muted: false,     //静音播放
          preload: 'none',  //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
          fluid: false,     //是否自适应布局,播放器将会有流体体积。换句话说，它将缩放以适应容器。         
          width: "850px",   //视频播放器的显示宽度（以像素为单位）（fluid=false时起作用）
          height: "600px",  //视频播放器的显示高度（以像素为单位）（fluid=false时起作用）
      },



    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询多模态数据列表 */
    getList() {
      this.loading = true;
      listMultimodal_file(this.queryParams).then(response => {
        this.multimodal_fileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 使用video.js组件播放视频
    showVideoWindow(){
      // 如果视频播放控件已经存在，切换视频url，重新播放；如果控件不存在，创建
      if(this.videoJsPlayer){
        this.videoJsPlayer.src([
          {
            src: this.playUrl,
            type: "video/mp4"
          }
        ]);
        if(this.videoPlayerOption.poster != null && this.videoPlayerOption.poster != ""){
          this.videoJsPlayer.poster(this.videoPlayerOption.poster);
        }
        this.videoJsPlayer.load(this.playUrl);
        // this.videoJsPlayer.play();  //自动播放
      }else{      
        this.$nextTick(() => {
          this.videoJsPlayer = this.videoJs(
            "casvideoplayer",          //播放器控件id
            this.videoPlayerOption     //播放器设置项（这里设置的poster属性不生效，需要在后面单独设置）
          );
          this.videoJsPlayer.poster(this.videoPlayerOption.poster);
        })
      }
    },
    // 取消按钮
    cancel() {
      // 重置视频控件数据（video.js组件）
      if(this.videoJsPlayer){
        this.videoJsPlayer.reset();
      }

      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        dataType: null,
        dataKey: null,
        avator: null,
        titleOrg: null,
        titleCn: null,
        publisher: null,
        summaryOrg: null,
        summaryCn: null,
        contentsOrg: null,
        contentsCn: null,
        url: null,
        localPath: null,
        labels: null,
        articleUrl: null,
        article: null,
        sourceId: null,
        fileFormat: null,
        lastTime: null,
        size: null,
        sourceType: null,
        uploadUserId: null,
        createTime: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加多模态数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMultimodal_file(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改多模态数据";
        var resoureUrl = "";
        if(typeof this.form.localPath != "undefined" && this.form.localPath != null && this.form.localPath != "") {
          resoureUrl = this.form.localPath;
        }else{
          resoureUrl = this.form.url;
        }

        if(this.form.dataType == 0 || this.form.dataType == "0"){          
          this.imgUrl = resoureUrl;
        }else if(this.form.dataType == 1 || this.form.dataType == "1"){      
          // video.js组件播放视频
          this.videoPlayerOption.poster = response.data.avator;
          this.playUrl = resoureUrl;
          console.log("视频参数：", this.playUrl);
          this.showVideoWindow();
        }else if(this.form.dataType == 2 || this.form.dataType == "2"){
          this.audioUrl = resoureUrl;
        }

        //修改属性使得下拉菜单显示正确
        this.form.dataType = this.form.dataType.toString()
        this.form.sourceType = this.form.sourceType.toString()
      });
    },
    // 修改界面显示多模态数据
    showMultimodalContent(row){
      if(row == null){
        return false;
      }
      return row.dataType;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMultimodal_file(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMultimodal_file(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除多模态数据编号为"' + ids + '"的数据项？').then(function() {
        return delMultimodal_file(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有多模态数据数据项？').then(() => {
        this.exportLoading = true;
        return exportMultimodal_file(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },

    // 单元格内容截取显示
    showContent(val){
      if(typeof val != 'undefined' && val != null){
        var MAX_LEN = 82;
        if(val.length > MAX_LEN){
          return val.substring(0, MAX_LEN) + "...";
        }
        else{
          return val;
        }
      }else{
        return "";
      }
    },

    //列表显示数据类型
    showDataType(dataType){
      var type = "";
      if(dataType == 0){
        type = "图片";
      }else if(dataType == 1){
        type = "视频";
      }else if(dataType == 2){
        type = "音频";
      }else if(dataType == 3){
        type = "文本";
      }
      return type;
    },

    //列表显示数据来源类型
    showDataSource(dataSource){
      var source = "";
      if(dataSource == 1){
        source = "用户上传";
      }else{
        source = "网络";
      }
      return source;
    },

  }
};
</script>

<style scoped>
/deep/ .video-js .vjs-fullscreen-control {
  position: absolute;
  top: -550px !important;
  left: 20px;
}
</style>
